#!/usr/bin/env python
"""Commandline utility to reduce Artillery heightmap levels so they render as
they did in older artillery versions.
"""
__author__ = "Ragzouken"
__copyright__ = "Copyright (C) 2008 Ragzouken"
__license__ = "Public Domain"
__version__ = "1.0"

from PIL import Image, PngImagePlugin
from optparse import OptionParser

_PARSER = OptionParser()
_PARSER.add_option("-m", "--map", help="Encode properties into MAP.", 
	dest="map", metavar="MAP")
OPTIONS, ARGS = _PARSER.parse_args()

def quantize(path):
	heightmap = Image.open(OPTIONS.map)
	meta = PngImagePlugin.PngInfo()
	pixels = heightmap.load()
	
	w, h = heightmap.size
	
	for y in xrange(h):
		for x in xrange(w):
			height = (pixels[x, y][0] // 15)
			pixels[x, y] = (height*15,) * 3
	
	for key, value in heightmap.info.iteritems():
		meta.add_text(key, value)
	
	heightmap.save(path, "png", pnginfo=meta)

if __name__ == '__main__':
	quantize(OPTIONS.map)
